<template>
	<view class="margin-sm">
		<view class=" " style="padding-bottom: 20px;">
			<uni-section title="选择作业" type="circle" class="padding-bottom-sm" v-if="index==0">
				<view class="padding-sm">
					已选：{{this.form.content.length}}个
				</view>
				<treeNode ref="treeNodeRef" @ppppp2="ppppp2">
				</treeNode>
			</uni-section>

			<uni-section title="完善作业" type="circle" class="padding-bottom-sm" v-if="index==1">
				<view v-for="item in form2.list" class="padding-sm ">

					<view class="padding-xs bg-gradual-green">{{ item.areaName }}</view>
					<view style="display: flex;align-items: center;" class="margin-top-sm">
						天数：<uni-easyinput v-model="item.days" :max="60" :min="1" type="number"></uni-easyinput>
					</view>

					<view v-if="item.remarks4.split('@').length===3&&item.remarks4.split('@')[2].split('#').length>0"
						class="margin-top-sm">
						<view v-for="item2 in item.remarks4.split('@')[2].split('#')"
							class="padding-xs margin-xs radius "
							style="border: .5px solid #000;display: flex;align-items: center;">
							<view style="border:#0009ff solid 1px ;background-color: #fff;width: 50rpx;height: 50rpx;"
								class="radius margin-right-sm " @click="dianjinumber(item,item2)">
								<uni-icons type="checkmarkempty" size="25" style="color: #0009ff;"
									v-if="item.number==item2"></uni-icons>
							</view>
							{{item2}}
						</view>
					</view>
					<view>
						<uni-easyinput v-model="item.remarks" placeholder="家长能看见的备注"></uni-easyinput>
					</view>

				</view>
				<uni-datetime-picker type="daterange" v-model="form.date" end-placeholder="结束日期"
					start-placeholder="开始日期" value-format="yyyy-MM-dd"></uni-datetime-picker>

			</uni-section>
		</view>
		<view style=" width: 100%; position: fixed;bottom: 0px;padding: 20rpx;" class="juzhong">
			<button @click="next(1)" v-if="index==0" style="width: 100%;" class=" cu-btn d3d bg-green  round ">
				下一步</button>
			<button @click="next(-1)" v-if="index==1" style="width: 30%;margin-right: 5%;"
				class=" cu-btn d3d bg-green  round ">
				上一步</button>

			<button @click="save" v-if="index==1" style="width: 60%;" class=" cu-btn d3d bg-blue  round ">
				保存</button>
		</view>
	</view>

</template>

<script>
	import {
		doDelete,
		doDeleteAll,
		doExportExcel,
		doUpdateOutTime,
		getGjzmsById,

		getJYPlan,
		getList,
		getUnfinishedMonthlyPlanInsert
	} from "@/api/lpc/monthlyplan/LpcMonthlyPlanManagementApi";

	import {
		customWorkAPI,
		doInsert,
		doUpdate,
		getById,
		getDateList
	} from "@/api/lpc/classassignments/LpcClassAssignmentsManagementApi";

	import {
		findTreeByid,
		getListByids
	} from "@/api/coreLessonPreparation/source";

	import {
		dateShifting,
		formateDate,
		isJson,
		isNull
	} from '@/utils/validate'
	import customerQuery from "@/pages/common/customerQuery/customerQuery.vue"
	import {
		formatterUserName,
		getcommon
	} from '@/utils/common'
	export default {
		components: {
			customerQuery
		},
		data() {
			return {
				xuanzeNumber: null,
				ids: [],
				index: 0,
				queryFrom: {},
				customWorkList: [],
				form: {
					date: [formateDate(new Date(), 'yyyy-MM-dd'), dateShifting(new Date(), 7)],
					outTime: 0,
					izUsable: '1',
					pushToParents: 0,
					content: [],
					remarks: '',
					customerId: '',
					status: '0',
					connectType: '1',
					createTime: null,
				},
				options: [],
				dict: {},
				form2: {
					list: []
				}
			}
		},
		mounted() {
			this.dict.offline_class_name = this.$getDictList("offline_class_name");
			this.dict.status = this.$getDictList("status");
			this.dict.no_yes = this.$getDictList("no_yes");
		},
		onLoad(op) {
			if (op.customerId != null) {
				this.form.customerId = op.customerId
				this.init()
			}

		},

		methods: {
			async save() {

				if (this.form.date.length !== 2) {
					this.$message.error("请选择作业的日期");
					return;
				}
				var aaaa = JSON.parse(JSON.stringify(this.form2.list))

				for (const adfasdElement of aaaa) {
					let asdfasdf = {
						content: JSON.stringify(
							adfasdElement
						),
						customerId: this.form.customerId,
						name: adfasdElement.areaName,
						staDate: this.form.date[0],
						outTime: this.form.date[1],
						days: adfasdElement.days,
						pushToParents: this.form.pushToParents,
						number: adfasdElement.number,
						remarks: adfasdElement.remarks
					}
					if (isNull(adfasdElement.id)) {
						asdfasdf.content = JSON.stringify(
							adfasdElement
						)
					} else {
						asdfasdf.content = adfasdElement.id

					}

					if (isNull(asdfasdf.number)) {
						uni.showToast({
							title: "次数不能为空",
							icon: "none"
						})
						return
					}

					if (isNull(asdfasdf.days)) {
						uni.showToast({
							title: "次数不能为空",
							icon: "none"
						})
						return
					}
					this.save2(asdfasdf)
				}
				// 处理数据
				// this.close();

			},
			async save2(data) {
				const {
					success,
					msg
				} = await doInsert(data);
				if (success) {


				}
			},
			dianjinumber(a, b) {
				a.number = b

				this.$forceUpdate()
			},
			next(i) {

				//如果是第一页 那就格式化选择的数据
				if (this.index === 0) {
					this.anjiagou()
					this.anguanjianci()
					this.zidingyi()
					if (this.ids.length > 20) {
						uni.showToast({
							title: "最多20个项目",
							icon: "none"
						})
						return
					}

					if (this.ids.length < 1) {
						uni.showToast({
							title: "至少有一个项目",
							icon: "none"
						})
						return
					}
					//通过ids查询出对应的信息
				}
				if (this.index === 1) {
					setTimeout(() => {
						console.log("显示");
						this.$refs.treeNodeRef.list = this.options
					}, 300);

				}
				this.index = this.index + i


			},
			ppppp2(e) {

				let ids = this.findParentIds(this.options, e.value)
				ids.pop()
				ids.reverse()
				ids.push(e.value)
				e.ids = ids
				if (this.form.content.length == 0) {
					this.form.content.push(e.ids)
					return
				}
				let asdf = false
				for (var i = 0; i < this.form.content.length; i++) {
					if (e.value == this.form.content[i].value) {
						asdf = true
						this.form.content.splice(i, 1)
						return
					}
				}
				if (!asdf) {
					this.form.content.push(e.ids)
				}
			},
			// 查找上级 ID 的函数
			findParentIds(tree, id, parentIds = []) {
				for (const node of tree) {
					if (node.value === id) {
						if (node.parentId !== null) {
							parentIds.push(node.parentId);
						}
						return parentIds; // 找到目标节点，返回当前的 parentIds
					}
					// 递归查找子节点
					if (node.children && node.children.length > 0) {
						const result = this.findParentIds(node.children, id, parentIds);
						if (result.length > 0) {
							if (node.parentId !== null) {
								parentIds.push(node.parentId); // 添加当前节点的 parentId
							}
							return parentIds; // 返回找到的结果
						}
					}
				}
				return parentIds; // 如果没有找到，返回空数组
			},
			init() {
				this.getterr()
			},
			async getterr() {
				this.options = await this.findTreeByid("1663093548330901506");
				for (let s of this.options) {
					s.is_level = 1
				}
				this.$refs.treeNodeRef.list = this.options
			},
			async findTreeByid(id) {
				const {
					success,
					msg,
					data
				} = await findTreeByid({
					"parentId": id,
					"type": "2"
				})
				return data
			},

			anjiagou() {
				this.ids = []

				for (const argument of this.form.content) {

					if (!this.ids.includes(argument[argument.length - 1])) {
						this.ids.push(argument[argument.length - 1])
					}

				}
				this.getListByids()
			},
			anguanjianci() {
				// this.ids = []
				// for (const argument of this.yixuanze_xiangmu) {
				// 	if (!this.ids.includes(argument.split(",")[argument.split(",").length - 1])) {
				// 		this.ids.push(argument.split(",")[argument.split(",").length - 1])
				// 	}
				// }
				// this.getListByids()
			},
			async getListByids() {
				console.log(this.ids);
				if (this.ids.length > 0) {
					const {
						data
					} = await getListByids({
						ids: this.ids.toString()
					})
					for (const datum of data) {
						datum.days = 1;
						const exists = this.form2.list.some(item => item.areaName == datum.areaName);
						if (!exists) {
							this.form2.list.push(datum)
						}

					}
				}

			},
			zidingyi() {
				// if (this.customWorkList.length > 0) {
				// 	for (const argument of this.customWorkList) {

				// 		let asdfasdf = argument.cishu.join("#")
				// 		const exists = this.form2.list.some(item => item.areaName == argument.title);

				// 		if (!exists) {
				// 			this.form2.list.push({
				// 				areaName: argument.title,
				// 				id: "",
				// 				days: 1,
				// 				remarks1: '',
				// 				remarks4: argument.fangfa + "@" + argument.zhuyi + "@" + asdfasdf,
				// 			})
				// 		}

				// 	}

				// }
			},

		}
	}
</script>

<style lang="scss">
	.navbar {
		width: 100%;
		background-color: #f1f1f1;
		z-index: 4;
		text-align: center;
		position: -webkit-sticky;
		position: -ms-sticky;
		position: -o-sticky;
		position: sticky;
		left: 0;
		top: 0
	}

	.mubiaoTitle {
		margin: 5rpx 0px;
	}
</style>